﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;

namespace Gp.Scripts.Core.Misc {
    public class DragUIZone : MonoBehaviour, IDragHandler {
        [FormerlySerializedAs("border")] [SerializeField]
        private RectTransform parentUI;

        [SerializeField] private int snap = 50;

        [SerializeField] private RectTransform leftBorder;
        [SerializeField] private RectTransform rightBorder;

        public void OnDrag(PointerEventData e) {
            if (!BorderJudge(e.position)) return;
            
            RectTransformUtility.ScreenPointToLocalPointInRectangle(parentUI.parent as RectTransform,
                new Vector2(e.position.x, 0), null, out Vector2 localPoint);


            float x = localPoint.x;
            // 设置UI左边界位置
            parentUI.offsetMin = new Vector2((int) (x / snap) * snap, parentUI.offsetMin.y);
        }
        

        private bool BorderJudge(Vector2 ePosition) {
            var curX = ePosition.x;
            var minX = leftBorder.position.x;
            var maxX = rightBorder.position.x;

            return curX > minX && curX < maxX;
        }
    }
}